package org.west.sky.experience.bigtalkdp.chapter16;

/**
 * @author: chz
 * @date: 2023/6/28
 * @description: 具体的迭代器-倒序遍历
 */
public class ConcreteIteratorDesc<T> extends Iterator<T> {

    private final Mist<T> mist;

    int current;

    public ConcreteIteratorDesc(Mist<T> mist) {
        this.mist = mist;
        this.current = mist.size() - 1;
    }

    @Override
    public T first() {
        return mist.get(mist.size() - 1);
    }

    @Override
    public T next() {
        if (current >= 0) {
            return mist.get(current--);
        }
        throw new IndexOutOfBoundsException(String.format("expect %d,but only %d", current, mist.size()));
    }

    @Override
    public boolean hasNext() {
        return current >= 0;
    }

    @Override
    public T current() {
        return mist.get(current);
    }
}
